// Larbin
// Sebastien Ailleret
// 07-12-01 -> 07-12-01

#include <iostream>  
 using namespace std;
#include <string.h>
#include <unistd.h>

#include "options.h"

#include "types.h"
#include "global.h"
#include "fetch/file.h"
#include "utils/text.h"
#include "utils/debug.h"
#include "interf/output.h"

/** A page has been loaded successfully
 * @param page the page that has been fetched
 */
void loaded (html *page) {
    // Here should be the code for managing everything
    // page->getHeaders() gives a char* containing the http headers
    // page->getPage() gives a char* containing the page itself
    // those char* are statically allocated, so you should copy
    // them if you want to keep them
    // in order to accept \000 in the page, you can use page->getLength()
#ifdef BIGSTATS
    cout << "fetched : ";
    page->getUrl()->print();
    // cout << page->getHeaders() << "\n" << page->getPage() << "\n";
#endif // BIGSTATS
    url *u = page->getUrl();
    char* host = u->getHost();
    
    // 创建网站目录
    int endFileName = strlen(saveDir);
    char* fileName = new char[endFileName+maxUrlSize+50];
    strcpy(fileName, saveDir);
    if (fileName[endFileName-1] != '/') fileName[endFileName++] = '/';
    strcpy(fileName+endFileName, host);
    
    if ( access(fileName,F_OK)==-1 && mkdir(fileName, S_IRWXU) != 0)
        perror("trouble while creating dir");
    // 不同类型的文件存在不同的子目录
    int len_dir= endFileName+strlen(host);
    fileName[len_dir]='/';
    strcpy(fileName+len_dir+1,page->fileType());
    if ( access(fileName,F_OK)==-1 && mkdir(fileName, S_IRWXU) != 0)
        perror("trouble while creating dir");
    
    // compute html filename
    int len = strlen(u->getFile());//url.substr( loc2+1 );
    char* fname = new char[len+1];
    strcpy(fname, u->getFile() );
    if( !fname ){
        fname = "xxx.html";
    }
    for( int i=0;fname[i];i++){
        if( fname[i]=='/' ){
            fname[i] = '-';
        }
    }

    int dir_len = strlen(fileName);
    int f_len = strlen(fname);
    char* htmlfile = new char[dir_len+f_len+2];
    strcpy(htmlfile, fileName);
    if (htmlfile[dir_len-1] != '/')
        htmlfile[dir_len++] = '/';
    strcpy(htmlfile+dir_len, fname);
    // write html
    int fd = creat(htmlfile, S_IRWXU);
    if (fd < 0) {
        cerr <<fname;
        cerr << "cannot open file " << htmlfile << "\n";
        exit(1);
    }
    ecrireBuff(fd, page->getPage(), page->getLength()); // 写html
    close(fd);
}

/** The fetch failed
 * @param u the URL of the doc
 * @param reason reason of the fail
 */
void failure (url *u, FetchError reason) {
  // Here should be the code for managing everything
#ifdef BIGSTATS
  cout << "fetched failed (" << (int) reason << ") : ";
  u->print();
#endif // BIGSTATS
}

/** initialisation function
 */
void initUserOutput () {
    mkdir(saveDir, S_IRWXU);
}

/** stats, called in particular by the webserver
 * the webserver is in another thread, so be careful
 * However, if it only reads things, it is probably not useful
 * to use mutex, because incoherence in the webserver is not as critical
 * as efficiency
 */
void outputStats(int fds) 
{
   ecrire(fds, "Nothing to declare");
}
